package edu.ucsd.cse135.gas.action.applicant;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import edu.ucsd.cse135.gas.logic.support.Database;
import edu.ucsd.cse135.gas.resources.Constants;

public class ApplicationAction extends Action {

	public ApplicationAction() {
		super();
	}
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		
		try {
			
			Connection conn = Database.open();
			
			String sql = "SELECT application_status FROM applicant WHERE mail = ?;";
			
			PreparedStatement preStmt = conn.prepareStatement(sql);
			preStmt.setString(1, request.getRemoteUser());
			
			ResultSet rs  = preStmt.executeQuery();
			
			if(rs.next())
			{
				return mapping.findForward(Constants.OLD_APPLICATION);
			}
			else
			{
				return mapping.findForward(Constants.NEW_APPLICATION);
			}
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
		
	}

}
